<template>
  <div :class="className" :style="{height:height,width:width}"></div>
</template>

<script>

  import echarts from 'echarts';
  require('echarts/theme/macarons');
  import resize from "@/views/dashboard/mixins/resize";
  import {getProjectHundredChaptersTotalChartPie} from "@/api/screen/screen1/data";

  export default {
    mixins: [resize],
    props: {
      className: {
        type: String,
        default: 'chart'
      },
      width: {
        type: String,
        default: '100%'
      },
      height: {
        type: String,
        default: '100%'
      },
      projectId: {
        type: Number,
        required: true
      }
    },
    data() {
      return {
        chart: null,
        data: [],
        legend: [],
      }
    },
    mounted() {
      this.$nextTick(() => {
        this.initChart();
      })
    },
    beforeDestroy() {
      if (!this.chart) {
        return
      }
      this.chart.dispose()
      this.chart = null
    },
    methods: {
      initChart() {
        this.setOptions();
      },
      setOptions() {
        this.data = [];
        console.log("项目ID：" + this.projectId);
        getProjectHundredChaptersTotalChartPie(this.projectId).then(response => {
          this.data = response.data;
          this.legend = response.legend;
          this.chart = echarts.init(this.$el, 'macarons');
          let option = {
            // title: {
            //   text: '百章合同金额占比统计',
            //   subtext: '单位（万元）',
            //   left: 'center'
            // },
            color: [
              "#37a2da",
              "#32c5e9",
              "#9fe6b8",
              "#ffdb5c",
              "#ff9f7f",
              "#fb7293",
              "#e7bcf3",
              "#8378ea"
            ],
            tooltip: {
              trigger: 'item',
              formatter: '{a} <br/>{b} : {c} ({d}%)'
            },
            legend: {
              // top: 'bottom'
              left: 'center',
              bottom: '10',
              data: this.legend
            },
            series: [
              {
                name: '单位（万元）',
                type: 'pie',
                radius: [10, 60],
                center: ['50%', '50%'],
                data: this.data,
                // roseType: 'area',
                // itemStyle: {
                //   borderRadius: 1
                // },
                animationEasing: 'cubicInOut',
                animationDuration: 2600
              }
            ]
          };
          this.chart.setOption(option);
          if (response.data.length == 0) {
            this.chart.showLoading({
              text: '暂无数据',
              showSpinner: false,
              textColor: 'rgba(0, 255, 255, 255)',
              // bgColor: '#2f4554',
              textStyle: {
                rich: {
                  backgroundColor: {
                    opacity: 1
                  }
                }
              },
              maskColor: '#0f1325',
              fontSize: '26px',
              fontWeight: 'bold',
              spinnerRadius:20
            })
          } else {
            this.chart.hideLoading();
          }
        });


        // this.chart = echarts.init(this.$el, 'macarons');
        // let option = {
        //   legend: {
        //     top: 'bottom'
        //   },
        //   // toolbox: {
        //   //   show: true,
        //   //   feature: {
        //   //     mark: { show: true },
        //   //     dataView: { show: true, readOnly: false },
        //   //     restore: { show: true },
        //   //     saveAsImage: { show: true }
        //   //   }
        //   // },
        //   tooltip: {
        //     trigger: 'item',
        //     formatter: '{a} <br/>{b} : {c} ({d}%)'
        //   },
        //   series: [
        //     {
        //       name: 'Nightingale Chart',
        //       type: 'pie',
        //       radius: [10, 50],
        //       center: ['50%', '40%'],
        //       roseType: 'area',
        //       itemStyle: {
        //         borderRadius: 8
        //       },
        //       data: [
        //         { value: 40, name: 'rose 1' },
        //         { value: 38, name: 'rose 2' },
        //         { value: 32, name: 'rose 3' },
        //         { value: 30, name: 'rose 4' },
        //         { value: 28, name: 'rose 5' },
        //         { value: 26, name: 'rose 6' },
        //         { value: 22, name: 'rose 7' },
        //         { value: 18, name: 'rose 8' }
        //       ]
        //     }
        //   ]
        // };
        // this.chart.setOption(option);
      }
    }
  }
</script>
